package edu.gatech.cs2340.tigershroud;

import android.content.Context;

/**
 * Handles all database administration calls for the TodoList database.
 * @author Cameron Hord
 *
 */
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class TodoSQLiteHelper extends SQLiteOpenHelper 
{
	public static final String TABLE_TODO = "todo";
	public static final String COLUMN_ID = "_id";
	public static final String COLUMN_UNAME = "user";
	public static final String COLUMN_TNAME = "task";
	public static final String COLUMN_DESCR = "description";
	public static final String COLUMN_DATE = "date";
	public static final String COLUMN_TIME = "time";
	public static final String COLUMN_DNUM = "dnum";
	public static final String COLUMN_CATE = "category";
	public static final String COLUMN_ADDR = "address";
	public static final String COLUMN_CITY = "city";
	public static final String COLUMN_STATE = "state";
	public static final String COLUMN_ZIP = "zipcode";
	public static final String COLUMN_STAT = "status";
	public static final String COLUMN_PRIOR = "priority";

	private static final String DATABASE_NAME = "todo.db";
	private static final int DATABASE_VERSION = 1;

	private static final String DATABASE_CREATE = "CREATE TABLE " + TABLE_TODO 
			+ "(" + COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT UNIQUE, " 
			+ COLUMN_UNAME + " VARCHAR(30) NOT NULL, " 
			+ COLUMN_TNAME + " VARCHAR(30) NOT NULL, " 
			+ COLUMN_DATE + " VARCHAR(10) NOT NULL, " 
			+ COLUMN_TIME + " VARCHAR(8) NOT NULL, " 
			+ COLUMN_DNUM + " LONG NOT NULL, "
			+ COLUMN_DESCR + " VARCHAR(100) NOT NULL, "  
			+ COLUMN_CATE + " VARCHAR(30) NOT NULL, " 
			+ COLUMN_ADDR + " VARCHAR(60) NOT NULL, " 
			+ COLUMN_CITY + " VARCHAR(10) NOT NULL, "  
			+ COLUMN_STATE + " VARCHAR(10) NOT NULL, "  
			+ COLUMN_ZIP + " VARCHAR(10) NOT NULL, " 
			+ COLUMN_STAT + " VARCHAR(1) NOT NULL, " 
			+ COLUMN_PRIOR + " VARCHAR(10) NOT NULL)";

	/**
	 * Constructor for the helper class. Also makes the database. 
	 * @param context A context thing. Used for creation of the db by the 
	 * super class.
	 */
	public TodoSQLiteHelper(Context context) 
	{
		super(context, DATABASE_NAME, null, DATABASE_VERSION);
	}
	
	/**
	 * Creates the database via the create string.
	 * @param database The database to create (really updates b/c of android's TABLE_TODO
	 * database management system)
	 */
	@Override
	public void onCreate(SQLiteDatabase database) 
	{
		database.execSQL(DATABASE_CREATE);
	}

	/**
	 * Upgrades the database to a new version. Wipes data.
	 * @param db The database to update
	 * @param oldVersion The old version of the db
	 * @param newVersion The new version of the db
	 */
	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) 
	{
		Log.w(TodoSQLiteHelper.class.getName(),
				"Upgrading database from version " + oldVersion + " to "
				+ newVersion + ", which will destroy all old data");
		db.execSQL("DROP TABLE IF EXISTS " + TABLE_TODO);
		onCreate(db);
	}
	
	public String getCreateStatement()
	{
		return DATABASE_CREATE;
	}
}